<?php

require_once "ConexaoMySql.php";

class UsuarioDAO {

    function criarUsuario($user, $password, $nome, $lastname) {
        $conn = new ConexaoMySql();
        $conn->conectarBdHMailServer();
        
        $query = mysql_query("INSERT INTO `hm_accounts` (`accountdomainid`,`accountadminlevel`,`accountaddress`,
        `accountpassword`,`accountactive`,`accountisad`,`accountaddomain`,`accountadusername`,
        `accountmaxsize`,`accountvacationmessageon`,`accountvacationmessage`,`accountvacationsubject`,
        `accountpwencryption`,`accountforwardenabled`,`accountforwardaddress`,`accountforwardkeeporiginal`,
        `accountenablesignature`,`accountsignatureplaintext`,`accountsignaturehtml`,
        `accountlastlogontime`,`accountvacationexpires`,`accountvacationexpiredate`,
        `accountpersonfirstname`,`accountpersonlastname`) 
        VALUES (1,0,'{$user}@webmail.com',MD5('{$password}'),
        1,0,'','',0,0,'','',2,0,'',0,0,'','','2012-01-04 11:05:45',0,now(),'{$nome}','${lastname}');");

        mysql_close();

        return $query;
    }
    
    function criarInboxFolder($userId){
        $conn = new ConexaoMySql();
        $conn->conectarBdHMailServer();

        $query = mysql_query("INSERT INTO `hm_imapfolders` (`folderaccountid`,`folderparentid`,`foldername`,
        `folderissubscribed`,`foldercreationtime`,`foldercurrentuid`) 
        VALUES ({$userId},-1,'INBOX',1,now(),0);");

        mysql_close();

        return $query;
    }
    
    function criarSentFolder($userId){
        $conn = new ConexaoMySql();
        $conn->conectarBdHMailServer();

        $query = mysql_query("INSERT INTO `hm_imapfolders` (`folderaccountid`,`folderparentid`,`foldername`,
        `folderissubscribed`,`foldercreationtime`,`foldercurrentuid`) 
        VALUES ({$userId},-1,'SENT',1,now(),0);");

        mysql_close();

        return $query;
    }
    
    function criarRascunhoFolder($userId){
        $conn = new ConexaoMySql();
        $conn->conectarBdHMailServer();

        $query = mysql_query("INSERT INTO `hm_imapfolders` (`folderaccountid`,`folderparentid`,`foldername`,
        `folderissubscribed`,`foldercreationtime`,`foldercurrentuid`) 
        VALUES ({$userId},-1,'RASCUNHO',1,now(),0);");

        mysql_close();

        return $query;
    }
    
    function obterUsuarioByLogin($username, $passwd) {
        $conn = new ConexaoMySql();
        $conn->conectarBdHMailServer();
        $query = mysql_query("SELECT accountid, accountaddress, accountpersonfirstname  
                FROM `hm_accounts` WHERE `accountaddress` = '{$username}@webmail.com' 
                and `accountpassword` = MD5('{$passwd}') LIMIT 1");

        $result = mysql_fetch_array($query);
        mysql_close();

        return $result;
    }
    
    function findById($userId) {
        $conn = new ConexaoMySql();
        $conn->conectarBdHMailServer();
        $account_res = mysql_query("SELECT accountid, accountaddress, accountpersonfirstname, accountpassword  
                FROM `hm_accounts` WHERE `accountid` = '{$userId}' LIMIT 1");
        $resposta =  mysql_fetch_array($account_res);
        mysql_close();
        return $resposta;
    }

    function verificarSeUsuarioExiste($username) {
        $conn = new ConexaoMySql();
        $conn->conectarBdHMailServer();
        $account_res = mysql_query("SELECT accountid, accountaddress, accountpersonfirstname  
                FROM `hm_accounts` WHERE `accountaddress` = '{$username}@webmail.com' LIMIT 1");
        $resposta = mysql_num_rows($account_res) == 1 ? true : false;
        mysql_close();
        return $resposta;
    }

}

?>
